//
//  NewUserRegisterVIew.m
//  Longer3D
//
//  Created by longer3d on 15/8/6.
//  Copyright (c) 2015年 longer3d. All rights reserved.
//

#import "NewUserRegisterVIew.h"
#import "NSString+Check.h"
#import "MBProgressHUD+MJ.h"



#define  labFont 15

@interface NewUserRegisterVIew() <UITextFieldDelegate>
@property(nonatomic,assign)int time;
@end

@implementation NewUserRegisterVIew

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundView = [[UIView alloc]initWithFrame:CGRectMake(12,12, ScreenWidth - 24, lineHeight *4)];
        self.backgroundView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.backgroundView];
        
        
        for (int i = 0; i<4; i++) {
            if ( i>0) {
                CALayer* line = [CALayer layer];
                line.frame = CGRectMake(12, lineHeight*i, self.backgroundView.bounds.size.width - 24, 1);
                line.backgroundColor = publicBackgroundColor.CGColor;
                [self.backgroundView.layer addSublayer:line];
            }
        }
        
        /*************手机号码************/
        
        UIImageView*loginiconImg = [[UIImageView alloc]initWithFrame:CGRectMake(12, 14, 20, 20)];
        loginiconImg.image = [UIImage imageNamed:@"login_phone_number"];
        [self.backgroundView addSubview:loginiconImg];
        
        self.usernameTF = [[UITextField alloc]initWithFrame:CGRectMake(37, 0, 200, lineHeight)];
        self.usernameTF.font = [UIFont systemFontOfSize:labFont];
        self.usernameTF.placeholder = @"手机号码";
        self.usernameTF.keyboardType = UIKeyboardTypeNumberPad;
        self.usernameTF.delegate = self;
        [self.backgroundView addSubview:self.usernameTF];
        
//        [self addImage:@"login_phone_number" line:0 TextField:self.usernameTF placeholder:@"手机号码"  secureText:NO keyboardType:UIKeyboardTypeNamePhonePad];

        /*************验证码*************/
        UIImageView *verificationcodeimg = [[UIImageView alloc]initWithFrame:CGRectMake(12, lineHeight+14, 20, 20)];
        verificationcodeimg.image = [UIImage imageNamed:@"security_code"];
        [self.backgroundView addSubview:verificationcodeimg];
        
        self.smscodeTF = [[UITextField alloc]initWithFrame:CGRectMake(37, lineHeight, 160,lineHeight)];
        self.smscodeTF.font = [UIFont systemFontOfSize:labFont];
        self.smscodeTF.placeholder = @"验证码";
        self.smscodeTF.keyboardType = UIKeyboardTypeNumberPad;
        [self.backgroundView addSubview:self.smscodeTF];
        
//        [self addImage:@"security_code" line:1 TextField:self.verificationcodeTF placeholder:@"验证码"  secureText:NO keyboardType:UIKeyboardTypeNumberPad];
        
        /*************设置密码*************/
        UIImageView* passwordImg = [[UIImageView alloc]initWithFrame:CGRectMake(12, lineHeight *2 +14, 20, 20)];
        passwordImg.image = [UIImage imageNamed:@"login_password"];
        [self.backgroundView addSubview:passwordImg];
        
        self.inputPasswdTF = [[UITextField alloc]initWithFrame:CGRectMake(37, lineHeight *2, 200, lineHeight)];
        self.inputPasswdTF.font = [UIFont systemFontOfSize:labFont];
        self.inputPasswdTF.placeholder = @"请输入密码";
        self.inputPasswdTF.secureTextEntry = YES;
        self.inputPasswdTF.delegate = self;
        [self.backgroundView addSubview:self.inputPasswdTF];


        
//        [self addImage:@"login_password" line:2 TextField:self.inputPasswdTF placeholder:@"请输入密码" secureText:YES keyboardType:UIKeyboardTypeDefault];
        
         /*************确认密码*************/
        
        UIImageView* confirmpwImg = [[UIImageView alloc]initWithFrame:CGRectMake(12, lineHeight *3 +14, 20, 20)];
        confirmpwImg .image = [UIImage imageNamed:@"login_sure_password"];
        [self.backgroundView addSubview:confirmpwImg ];
        
        self.confirmPasswdTF = [[UITextField alloc]initWithFrame:CGRectMake(37, lineHeight *3, 200, lineHeight)];
        self.confirmPasswdTF.font = [UIFont systemFontOfSize:labFont];
        self.confirmPasswdTF.placeholder = @"确认输入密码";
        self.confirmPasswdTF.secureTextEntry = YES;
        self.confirmPasswdTF.delegate = self;
        [self.backgroundView addSubview:self.confirmPasswdTF];
      

         /*************获取验证码*************/
        UIButton*verificationcodeBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.backgroundView.width -190/2 , lineHeight+7, 154/2, 32)];
        [verificationcodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        verificationcodeBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [verificationcodeBtn setTitleColor:publicBtnColor forState:UIControlStateNormal];
        verificationcodeBtn.layer.borderColor = publicBtnColor.CGColor;
        verificationcodeBtn.layer.borderWidth = 1;
        verificationcodeBtn.tag = 110;
        [verificationcodeBtn addTarget:self action:@selector(getSmscode:) forControlEvents:UIControlEventTouchUpInside];
        [self.backgroundView addSubview:verificationcodeBtn];
        
        //注册按钮
        self.registerBtn = [[UIButton alloc]initWithFrame:CGRectMake(12, self.backgroundView.frame.origin.y + self.backgroundView.frame.size.height+ 22, ScreenWidth - 24, 42)];
        [self.registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.registerBtn setTitle:@"注册" forState:UIControlStateNormal];
        [self.registerBtn setBackgroundColor:publicBtnColor];
        self.registerBtn.layer.cornerRadius = 4;
        [self.registerBtn addTarget:self action:@selector(registerClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.registerBtn];

    }
    return self;
}

- (void)registerClick
{
 
    
    if (![self.usernameTF.text checkPhoneNumInput]) {
        [MBProgressHUD showError:@"请输入正确的手机号"];
        return;
    }
    
    if (self.inputPasswdTF.text.length < 6) {
        [MBProgressHUD showError:@"密码不能少于6位"];
        return;
    }
    
    if (![self.inputPasswdTF.text isEqualToString:self.confirmPasswdTF.text]) {
        [MBProgressHUD showError:@"确认密码与输入密码不一致"];
        return;
    }
    
    
    if ([self.delegate respondsToSelector:@selector(registViewRegiterWithUsername:SmsCode:Password:RepeatPW:)]) {
        [self.delegate registViewRegiterWithUsername:self.usernameTF.text SmsCode:self.smscodeTF.text Password:self.inputPasswdTF.text RepeatPW:self.confirmPasswdTF.text];
    }

}




-(void)getSmscode:(UIButton*)sender{
    
    
    [self endEditing:YES];
  
    if (![self.usernameTF.text checkPhoneNumInput] ) {
        [MBProgressHUD showError:@"请输入正确的手机号码"];
        return;
    }

    if ([self.delegate respondsToSelector:@selector(registViewSendSmsCode:)]) {
        [self.delegate registViewSendSmsCode:self.usernameTF.text];
    }
    
    self.time = 60;
    sender.userInteractionEnabled = NO;
    [sender setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    sender.layer.borderColor = [UIColor grayColor].CGColor;
    sender.layer.borderWidth = 1;
    
    self.timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(12, self.registerBtn.frame.size.height + self.registerBtn.frame.origin.y +15, ScreenWidth - 24, 42)];
    self.timeLabel.backgroundColor = Loger3dColor(220, 220, 220, 0.3);
    self.timeLabel.text = @"验证码已发送 60S";
    self.timeLabel.textAlignment = NSTextAlignmentCenter;
    self.timeLabel.textColor = Loger3dColor(180, 180, 180, 1);
    [self addSubview:self.timeLabel];
    
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeaction:) userInfo:self.timeLabel repeats:YES];
    
}

-(void)timeaction:(NSTimer*)timer{
    UILabel*timelabel = timer.userInfo;
    timelabel.text = [NSString stringWithFormat:@"验证码已发送 %ldS",(long)self.time--];
    if (self.time < -1) {
        [timelabel removeFromSuperview];
        UIButton*verificationcodeBtn = (UIButton*)[self viewWithTag:110];
        verificationcodeBtn.userInteractionEnabled = YES;
        [verificationcodeBtn setTitleColor:[UIColor colorWithRed:13.0/99.0 green:101.0/255.0 blue:252.0/255.0 alpha:1] forState:UIControlStateNormal];
        verificationcodeBtn.layer.borderColor = [UIColor colorWithRed:13.0/99.0 green:101.0/255.0 blue:252.0/255.0 alpha:1].CGColor;
        verificationcodeBtn.layer.borderWidth = 1;
        [timer invalidate];
    }
}



- (void)addImage:(NSString *)image line:(NSInteger)i TextField:(UITextField *)textfield placeholder:(NSString*)placeholder secureText:(BOOL)secure  keyboardType:(UIKeyboardType)type
{
    UIImageView* imageView = [[UIImageView alloc]initWithFrame:CGRectMake(12, i*lineHeight+14, 13, 20)];
    imageView.image = [UIImage imageNamed:image];
    [self.backgroundView addSubview:imageView];
    
    textfield.keyboardType = type;
    textfield = [[UITextField alloc]initWithFrame:CGRectMake(37, lineHeight *i, 200, lineHeight)];
    textfield.font = [UIFont systemFontOfSize:labFont];
    textfield.placeholder = placeholder;
    textfield.secureTextEntry = secure;
    textfield.delegate = self;
    [self.backgroundView addSubview:textfield];
}




#pragma mark - 代理
- (void)textFieldDidBeginEditing:(UITextField *)textField
{

}

- (void)textFieldDidEndEditing:(UITextField *)textField
{

}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    string = [string uppercaseString];
    if ([string isEqualToString:@" "])  //按删除键可以改变
    {
        return NO;
    }
    
    if ([string isEqualToString:@"\n"])  //按删除键可以改变
    {
        return YES;
    }
    
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容
    
    if (self.usernameTF == textField)  //判断是否时我们想要限定的那个输入框
    {
        //限制只能输入数字
        NSUInteger lengthOfString = string.length;
        for (NSInteger loopIndex = 0; loopIndex < lengthOfString; loopIndex++) {//只允许数字输入
            unichar character = [string characterAtIndex:loopIndex];
            if (character < 48) return NO; // 48 unichar for 0
            if (character > 57) return NO; // 57 unichar for 9
        }
    
        if ([toBeString length] > 11) {
            return NO;
        }
    }

    return YES;
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self  endEditing:YES];
}

@end
